package com.ruoyi.food.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.environment.domain.OpmEnvRule;
import com.ruoyi.food.domain.OpmFoodSafeSystem;
import com.ruoyi.food.mapper.OpmFoodSafeSystemMapper;
import com.ruoyi.food.service.IOpmFoodSafeSystemService;
import com.ruoyi.system.api.LinkFileServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 管理体系Service业务层处理
 *
 * @author cjie
 * @date 2021-07-24
 */
@Service
public class OpmFoodSafeSystemServiceImpl extends LinkFileServiceImpl implements IOpmFoodSafeSystemService {
    @Autowired
    private OpmFoodSafeSystemMapper opmFoodSafeSystemMapper;

    /**
     * 查询管理体系
     *
     * @param id 管理体系ID
     * @return 管理体系
     */
    @Override
    public OpmFoodSafeSystem selectOpmFoodSafeSystemById(Long id) {
        OpmFoodSafeSystem result = opmFoodSafeSystemMapper.selectOpmFoodSafeSystemById(id);
        result.setFileList(getFileList(id.toString()));
        return result;
    }

    /**
     * 查询管理体系列表
     *
     * @param opmFoodSafeSystem 管理体系
     * @return 管理体系
     */
    @Override
    public List<OpmFoodSafeSystem> selectOpmFoodSafeSystemList(OpmFoodSafeSystem opmFoodSafeSystem) {
        return opmFoodSafeSystemMapper.selectOpmFoodSafeSystemList(opmFoodSafeSystem);
    }

    /**
     * 新增管理体系
     *
     * @param opmFoodSafeSystem 管理体系
     * @return 结果
     */
    @Override
    public int insertOpmFoodSafeSystem(OpmFoodSafeSystem opmFoodSafeSystem) {
        opmFoodSafeSystem.setId(IdUtil.getSnowflake(0, 0).nextId());
        opmFoodSafeSystem.setCreateTime(DateUtils.getNowDate());
        opmFoodSafeSystem.setCreateBy(SecurityUtils.getUsername());
        linkFile(opmFoodSafeSystem.getFileIdList(), opmFoodSafeSystem.getId().toString());
        return opmFoodSafeSystemMapper.insertOpmFoodSafeSystem(opmFoodSafeSystem);
    }

    /**
     * 修改管理体系
     *
     * @param opmFoodSafeSystem 管理体系
     * @return 结果
     */
    @Override
    public int updateOpmFoodSafeSystem(OpmFoodSafeSystem opmFoodSafeSystem) {
        opmFoodSafeSystem.setUpdateTime(DateUtils.getNowDate());
        opmFoodSafeSystem.setUpdateBy(SecurityUtils.getUsername());
        linkFile(opmFoodSafeSystem.getFileIdList(), opmFoodSafeSystem.getId().toString());
        return opmFoodSafeSystemMapper.updateOpmFoodSafeSystem(opmFoodSafeSystem);
    }

    /**
     * 批量删除管理体系
     *
     * @param ids 需要删除的管理体系ID
     * @return 结果
     */
    @Override
    public int deleteOpmFoodSafeSystemByIds(Long[] ids) {
        String[] res = new String[ids.length];
        for(int i=0;i<ids.length;i++){
            res[i] = String.valueOf(ids[i]);
        }
        deleteLinkFile(res);
        return opmFoodSafeSystemMapper.deleteOpmFoodSafeSystemByIds(ids);
    }

    /**
     * 删除管理体系信息
     *
     * @param id 管理体系ID
     * @return 结果
     */
    @Override
    public int deleteOpmFoodSafeSystemById(Long id) {
        return opmFoodSafeSystemMapper.deleteOpmFoodSafeSystemById(id);
    }
}
